package server;

import java.io.InputStream;
import java.net.Socket;

public class RequestProcessor extends Thread {

    private Socket socket;
    private Mapper.MappedHost mappedHost;

    public RequestProcessor(Socket socket, Mapper.MappedHost mappedHost) {
        this.socket = socket;
        this.mappedHost = mappedHost;
    }


    @Override
    public void run() {
        try {
            InputStream inputStream = socket.getInputStream();

            // 封装Request对象和Response对象
            Request request = new Request(inputStream);
            Response response = new Response(socket.getOutputStream());
            Mapper.MappedWrapper mappedWrapper = null;
            String url = request.getUrl().substring(1);
            int index = url.indexOf("/");
            if (index >= 0) {
                String contextName = url.substring(0, index);
                String wrapperName = url.substring(index);
                //获取MappedContext
                Mapper.MappedContext mappedContext = getMappedContext(contextName);

                if (mappedContext != null) {
                    //获取MappedWrapper
                    mappedWrapper = getMappedWrapper(mappedContext, wrapperName);
                }
                System.out.println(contextName + ":" + wrapperName);
            }
            // 静态资源处理
            if (mappedWrapper == null) {
                response.outputHtml(request.getUrl());
            } else {
                // 动态资源servlet请求
                HttpServlet httpServlet = (HttpServlet) mappedWrapper.object.getServlet();
                httpServlet.service(request, response);
            }
            socket.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public Mapper.MappedContext getMappedContext(String contextName) {
        for (Mapper.MappedContext context : mappedHost.object.contexts) {
            if (context.name.equalsIgnoreCase(contextName)) {
                return context;
            }
        }
        return null;
    }

    public Mapper.MappedWrapper getMappedWrapper(Mapper.MappedContext mappedContext, String wrapperName) {
        for (Mapper.MappedWrapper mappedWrapper : mappedContext.object) {
            if (mappedWrapper.name.equalsIgnoreCase(wrapperName)) {
                return mappedWrapper;
            }
        }
        return null;
    }

//    @Override
//    public void run() {
//        try{
//            InputStream inputStream = socket.getInputStream();
//
//            // 封装Request对象和Response对象
//            Request request = new Request(inputStream);
//            Response response = new Response(socket.getOutputStream());
//
//            // 静态资源处理
//            if(servletMap.get(request.getUrl()) == null) {
//                response.outputHtml(request.getUrl());
//            }else{
//                // 动态资源servlet请求
//                HttpServlet httpServlet = servletMap.get(request.getUrl());
//                httpServlet.service(request,response);
//            }
//
//            socket.close();
//
//        }catch (Exception e) {
//            e.printStackTrace();
//        }
//
//    }
}
